﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private List<Employee> EmployeeList;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EmployeeList = new List<Employee>();
            EmployeeList.Add(new Employee(1, "Jump", "Dan"));
            EmployeeList.Add(new Employee(2, "Kirwan", "Yvette"));
            ViewState["EmployeeList"] = EmployeeList;
        }
        else
            EmployeeList = (List<Employee>)ViewState["EmployeeList"];

        EmployeesGridView.DataSource = EmployeeList;
        EmployeesGridView.DataBind();
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(FirstNameTextBox.Text) ||
           String.IsNullOrEmpty(LastNameTextBox.Text)) { return; }

        int employeeID = EmployeeList[EmployeeList.Count - 1].EmployeeID + 1;

        string lastName = Server.HtmlEncode(FirstNameTextBox.Text);
        string firstName = Server.HtmlEncode(LastNameTextBox.Text);

        FirstNameTextBox.Text = String.Empty;
        LastNameTextBox.Text = String.Empty;

        EmployeeList.Add(new Employee(employeeID, lastName, firstName));
        ViewState["EmployeeList"] = EmployeeList;

        EmployeesGridView.DataBind();
        EmployeesGridView.PageIndex = EmployeesGridView.PageCount;
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        FirstNameTextBox.Text = String.Empty;
        LastNameTextBox.Text = String.Empty;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Employee> col = (List<Employee>)this.EmployeesGridView.DataSource;

        Response.Write("Datos del servidor <br />");
        foreach (Employee emp in col)
        {
            Response.Write(String.Format("id:{0} FirstName:{1} LastName:{2} <br />", emp.EmployeeID, emp.FirstName, emp.LastName));
        }

    }
}
[Serializable]
public class Employee
{
    private int _employeeID;
    private string _lastName;
    private string _firstName;

    public int EmployeeID
    {
        get { return _employeeID; }
    }

    public string LastName
    {
        get { return _lastName; }
    }

    public string FirstName
    {
        get { return _firstName; }
    }

    public Employee(int employeeID, string lastName, string firstName)
    {
        _employeeID = employeeID;
        _lastName = lastName;
        _firstName = firstName;
    }
}
